var uid = getParameter("uid");
var day = getParameter("Day");
var now = new Date();
var arg = 1;
var code=getParameter("code");
$(function() {
	if(uid){
	charts(now);
	}else{
		getToken(code);
	}
});
function getToken(code_){
	var urlStr = serverPath + "/server/doctor/diabetes/login.do";
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"",'+
	    '	   "method":"taipinglogin",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '	         "code":"'+code_+'"'+
	    '	      }'+
	    '	   ]'+
	    '	}';
	
	UseAjax(urlStr, jsonStr, document.cookie, getUid, '1');

};

function getUid(data) {
	if(data.resultcode=='0'){
		uid = data.uid;
		charts(now);
	}else{
		alert(data.resultmsg);	
	}
}
function charts(now) {
	var befdate;
	var aftdate;
	if (day == 30) {
		befdate = getLastDay(now, 29).format("yyyy-MM-dd");
	    aftdate = getNextDay(now, 1).format("yyyy-MM-dd");
		arg = 15;
	} else if (day == 7) {
		befdate = getLastDay(now, 6).format("yyyy-MM-dd");
	    aftdate = getNextDay(now, 1).format("yyyy-MM-dd");
	}else if(day == 1){
		befdate = now.format("yyyy-MM-dd");
	    aftdate = now.format("yyyy-MM-dd");
	}
	var urlStr = serverPath + "/app/doctor/diabetes/mealView.do";
	/*var jsonStr = '{method:"getMealTable",UId:"' + UId + '",date:"'
			+ now.format("yyyy-MM-dd") + '",day:"' + day + '",befdate:"'
			+ getLastDay(now, arg).format("yyyy-MM-dd") + '",aftdate:"'
			+ getNextDay(now, arg).format("yyyy-MM-dd") + '"}';*/
	
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"'+uid+'",'+
	    '	   "method":"getMealTable",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '	         "uid":"'+uid+'",'+
	    '            "date":"' + now.format("yyyy-MM-dd")  +'",'+
	    '            "befdate":"' + befdate +'",'+
	    '            "aftdate":"' + aftdate +'",'+
	    '            "day":"'+ day +'"'+
	    '	      }'+
	    '	   ]'+
	    '	}';
	
	
	UseAjax(urlStr, jsonStr, document.cookie, rt, '1');
}
function rt(data) {
	var records = data.resultlist[0].records;
	var html = "";
	html = '<table class="bordered"><thead> '
			+ '<tr><th>日期</th><th>早餐</th><th>午餐</th><th>晚餐</th><th>加餐</th><th>合计</th> '
			+ '</tr></thead>';
	for (var int = 0; int < records.length; int++) {
		var wi = records[int];// .substring(0,10)
		html += ' <tr><td>' + wi.dates.substring(5) + '</td><td>' + isnull(wi.breakfast) + '</td><td>'
				+ isnull(wi.lunch) + '</td><td>' + isnull(wi.dinner) + '</td><td>' + isnull(wi.snack)
				+ '</td><td>' + eval(Number(wi.breakfast)+Number(wi.lunch)+Number(wi.dinner)+Number(wi.snack)) + '</td></tr>';
	}
	$("#container").html(html);
	//设置表格颜色
	$("tr:odd").each(function(index) {
		$(this).css("background-color","#e0ecfb");
	});
	$("tr:first").css("color","#ffffff");
}

$(function() {
	// swipe 的dome
	$("#container").swipe(
			{
				swipeLeft : function(event, direction, distance, duration,
						fingerCount) {

					now = getNextDay(now, arg);
					charts(now);
				},
				swipeRight : function(event, direction, distance, duration,
						fingerCount) {
					now = getLastDay(now, arg);
					charts(now);
				}
			});

});
function isnull(value) {
	if (value == null || value == "" || value == undefined) {
		return "";
	} else {
		return value;
	}
}